package com.ncepu_ideal.maven_testng;

import com.ncepu_ideal.maven_testng.controller.StudentController;
import com.ncepu_ideal.maven_testng.model.Student;
import org.json.JSONObject;

import java.util.Scanner;

/**
 * @description:
 * @author: ChengKai
 * @time: 2023/3/2 10:15
 */
public class Start {
    public static void main(String[] args) {
        while (true) {
            System.out.println(
                      "  --------------\n"
                    + " |  1 添加数据； |\n"
                    + " |  2 查询数据； |\n"
                    + " |  3 修改数据； |\n"
                    + " |  4 删除数据； |\n"
                    + " |  0 退出程序； |\n"
                    + "  --------------\n"
                    + "请输入相应的命令菜单："
            );

            Scanner sc = new Scanner(System.in);
            int num = 0;
            Student s = new Student();

            try {
                num = sc.nextInt();
                sc.nextLine();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("输入有误");
                continue;
            }

            if (num == 1) {
                try {
                    System.out.println("请输入需要添加的学生记录:");
                    String info = sc.nextLine();
                    String[] infoArray = info.split(" ");
                    s.setStudentId(infoArray[0]);
                    s.setSName(infoArray[1]);
                    s.setSAge(Integer.parseInt(infoArray[2]));
                    s.setSSex(infoArray[3]);
                    s.setDeptId(infoArray[4]);
                    s.setSClass(infoArray[5]);

                    System.out.println(new JSONObject(StudentController.addBySql(s)));
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("输入有误");
                }

            } else if (num == 2) {
                System.out.println("请输入学号:");
                String studentId = sc.nextLine();

                System.out.println(new JSONObject(StudentController.searchBySqlId(studentId)));

            } else if (num == 3) {
                System.out.println("请输入需要修改的学生记录:");
                String infoUpdate = sc.nextLine();
                String[] infoArrayUpdate = infoUpdate.split(" ");
                s.setStudentId(infoArrayUpdate[0]);
                s.setSName(infoArrayUpdate[1]);
                s.setSAge(Integer.parseInt(infoArrayUpdate[2]));
                s.setSSex(infoArrayUpdate[3]);
                s.setDeptId(infoArrayUpdate[4]);
                s.setSClass(infoArrayUpdate[5]);

                System.out.println(new JSONObject (StudentController.updateBySql(s)));
            } else if (num == 4) {
                System.out.println("请输入学号:");
                String studentID = sc.nextLine();

                System.out.println(new JSONObject(StudentController.deleteBySqlId(studentID)));
            } else {
                System.exit(0);
                break;
            }

        }
    }
}
